//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartContainerView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartContainerView.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartContainerView

IMPLEMENT_DYNCREATE(CChartContainerView, CBCGPChartExampleView)

CChartContainerView::CChartContainerView()
	: CBCGPChartExampleView(CChartContainerView::IDD)
{
	//{{AFX_DATA_INIT(CChartContainerView)
	m_bShowCaption = TRUE;
	m_bShowLegend = TRUE;
	//}}AFX_DATA_INIT

	m_pCaption = NULL;
	m_pChart1 = NULL;
	m_pChart2 = NULL;
	m_pChart3 = NULL;
}

CChartContainerView::~CChartContainerView()
{
}

void CChartContainerView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartContainerView)
	DDX_Control(pDX, IDC_CHART, m_wndContainer);
	DDX_Check(pDX, IDC_SHOW_CAPTION, m_bShowCaption);
	DDX_Check(pDX, IDC_SHOW_LEGEND, m_bShowLegend);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartContainerView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartContainerView)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_SHOW_LEGEND, OnUpdateChart)
	ON_BN_CLICKED(IDC_SHOW_CAPTION, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartContainerView diagnostics

#ifdef _DEBUG
void CChartContainerView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartContainerView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartContainerView message handlers

void CChartContainerView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	CBCGPVisualContainer* pContainer = m_wndContainer.GetVisualContainer();
	ASSERT_VALID(pContainer);

	m_pCaption = new CBCGPTextGaugeImpl(_T("Container:"), 
		globalData.m_bIsBlackHighContrast ? CBCGPColor::White : CBCGPColor::Black, pContainer);
	
	CBCGPTextFormat tf(_T("Arial"), 20);
	tf.SetTextAlignment(CBCGPTextFormat::BCGP_TEXT_ALIGNMENT_LEADING);
	tf.SetUnderline();

	m_pCaption->SetTextFormat(tf);

	m_pChart1 = new CBCGPChartVisualObject(pContainer);

	m_pChart1->SetChartType(BCGPChartBar);
	m_pChart1->SetLegendPosition(BCGPChartLayout::LP_NONE);
	m_pChart1->ShowAxisIntervalInterlacing(BCGP_CHART_X_PRIMARY_AXIS);
	
	m_pChart1->AddChartData(1.);
	m_pChart1->AddChartData(2.);
	m_pChart1->AddChartData(5.);
	m_pChart1->AddChartData(3.);
	m_pChart1->AddChartData(2.);
	m_pChart1->AddChartData(4.);

	m_pChart2 = new CBCGPChartVisualObject(pContainer);

	m_pChart2->SetChartType(BCGPChartDoughnut3D);
	
	m_pChart2->AddChartData(1.);
	m_pChart2->AddChartData(2.);
	m_pChart2->AddChartData(5.);
	m_pChart2->AddChartData(3.);
	m_pChart2->AddChartData(2.);

	for (int i = 0; i < m_pChart2->GetSeriesCount(); i++)
	{
		m_pChart2->GetSeries(i)->SetDefaultFillGradientType(CBCGPBrush::BCGP_GRADIENT_BEVEL);
	}

	m_pChart3 = new CBCGPChartVisualObject(pContainer);

	m_pChart3->SetChartType(BCGPChartArea);
	m_pChart3->ShowAxisIntervalInterlacing(BCGP_CHART_X_PRIMARY_AXIS);

	m_pChart3->AddChartData(0.);
	m_pChart3->AddChartData(1.);
	m_pChart3->AddChartData(2.);
	m_pChart3->AddChartData(5.);
	m_pChart3->AddChartData(3.);
	m_pChart3->AddChartData(2.);
	m_pChart3->AddChartData(4.);
	m_pChart3->AddChartData(3.);
	m_pChart3->AddChartData(8.);
	m_pChart3->AddChartData(7.);
	m_pChart3->AddChartData(9.);
	m_pChart3->AddChartData(6.);
	m_pChart3->AddChartData(11.);
	m_pChart3->AddChartData(5.);
	m_pChart3->AddChartData(0.);

	m_pChart3->SetCurveType(BCGPChartFormatSeries::CCT_SPLINE);
	m_pChart3->SetThemeOpacity(60);

	m_pChart3->ShowDataMarkers();
	m_pChart3->ShowDataLabels();

	Repos();

	CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
	UpdateChartColorTheme(pMainFrame->GetColorTheme(), pMainFrame->IsDarkTheme());
}

void CChartContainerView::OnUpdateChart()
{
	UpdateData();

	m_pChart2->SetLegendPosition(m_bShowLegend ? BCGPChartLayout::LP_RIGHT : BCGPChartLayout::LP_NONE);
	m_pChart3->SetLegendPosition(m_bShowLegend ? BCGPChartLayout::LP_RIGHT : BCGPChartLayout::LP_NONE);

	Repos();
}

void CChartContainerView::OnSize(UINT nType, int cx, int cy) 
{
	CBCGPChartExampleView::OnSize(nType, cx, cy);
	Repos();
}

void CChartContainerView::Repos()
{
	CBCGPVisualContainer* pContainer = m_wndContainer.GetVisualContainer();
	ASSERT_VALID(pContainer);

	if (pContainer == NULL || pContainer->GetCount() == 0)
	{
		return;
	}

	const double margin = 10;

	CBCGPRect rect = pContainer->GetRect();

	if (m_bShowCaption)
	{
		const double captionHeight = 20;

		CBCGPRect rectCaption = rect;
		rectCaption.bottom = rectCaption.top + captionHeight + margin;

		rect.top = rectCaption.bottom;

		rectCaption.DeflateRect(margin, 0);
		m_pCaption->SetRect(rectCaption);
		m_pCaption->SetVisible();
	}
	else
	{
		m_pCaption->SetVisible(FALSE);
	}

	CBCGPRect rectChart1 = rect;
	rectChart1.SetSize(rect.Width() / 2, rect.Height() / 2);
	rectChart1.DeflateRect(margin, margin);

	m_pChart1->SetRect(rectChart1);

	CBCGPRect rectChart2 = rectChart1;
	rectChart2.OffsetRect(rect.Width() / 2, 0);

	m_pChart2->SetRect(rectChart2);

	CBCGPRect rectChart3 = rect;
	rectChart3.top += rect.Height() / 2;
	rectChart3.DeflateRect(margin, margin);

	m_pChart3->SetRect(rectChart3);

	m_wndContainer.RedrawWindow();
}

void CChartContainerView::UpdateChartColorTheme(int nTheme, BOOL bIsDarkTheme)
{
	CBCGPBrush brFill;

	if (m_pChart1 != NULL)
	{
		if (nTheme < 0)
		{
			m_pChart1->SetColors(theApp.GetCustomTheme(nTheme), bIsDarkTheme);
			brFill = theApp.GetCustomTheme(nTheme).m_brChartFillColor;
		}
		else
		{
			m_pChart1->SetColors((CBCGPChartTheme::ChartTheme)nTheme, bIsDarkTheme);
			brFill = m_pChart1->GetColors().m_brChartFillColor;
		}

		if (m_pCaption != NULL && !globalData.m_bIsBlackHighContrast)
		{
			m_pCaption->SetTextColor(m_pChart1->GetColors().m_brTitleTextColor.GetColor());
		}
	}

	if (m_pChart2 != NULL)
	{
		if (nTheme < 0)
		{
			m_pChart2->SetColors(theApp.GetCustomTheme(nTheme), bIsDarkTheme);
			brFill = theApp.GetCustomTheme(nTheme).m_brChartFillColor;
		}
		else
		{
			m_pChart2->SetColors((CBCGPChartTheme::ChartTheme)nTheme, bIsDarkTheme);
			brFill = m_pChart2->GetColors().m_brChartFillColor;
		}
	}

	if (m_pChart3 != NULL)
	{
		if (nTheme < 0)
		{
			m_pChart3->SetColors(theApp.GetCustomTheme(nTheme), bIsDarkTheme);
			brFill = theApp.GetCustomTheme(nTheme).m_brChartFillColor;
		}
		else
		{
			m_pChart3->SetColors((CBCGPChartTheme::ChartTheme)nTheme, bIsDarkTheme);
			brFill = m_pChart3->GetColors().m_brChartFillColor;
		}
	}

	m_wndContainer.GetVisualContainer()->SetFillBrush(bIsDarkTheme ? CBCGPBrush() : brFill);

	m_wndContainer.RedrawWindow();
}

void CChartContainerView::OnChartAnimation()
{
	if (m_pChart1 != NULL)
	{
		m_pChart1->StartAnimation(theApp.m_dblChartAnimationTime, theApp.m_animationType, theApp.m_animationStyle);
	}
	
	if (m_pChart2 != NULL)
	{
		m_pChart2->StartAnimation(theApp.m_dblChartAnimationTime, theApp.m_animationType, theApp.m_animationStyle);
	}
	
	if (m_pChart3 != NULL)
	{
		m_pChart3->StartAnimation(theApp.m_dblChartAnimationTime, theApp.m_animationType, theApp.m_animationStyle);
	}
}
